#include "commonpage.h"
#include "listitembox.h"
#include "ui_commonpage.h"
#include<QListWidgetItem>
CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->playAllBtn, &QPushButton::clicked, this, [=](){
     emit playAll(pageType);
     });
    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const
    QModelIndex &index){
    emit playMusicByIndex(this, index.row());
     });
}
void CommonPage::setCommonPageUI(const QString &title, const QString &image)
 {
     // 设置标题
    ui->pageTittle->setText(title);
    // 设置封⾯栏
    ui->musicImageLabel->setPixmap(QPixmap(image));
    ui->musicImageLabel->setScaledContents(true);

}
void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();
    addMusicToMusicPage(musicList);
    for(auto musicId : musicListOfPage)
     {
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end())
        continue;
        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSingerName(it->getSingexrName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeIcon(it->getIsLike());
        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(listItemBox->width(), listItemBox->height()));
        ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);

        connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike){
         emit updateLikeMusic(isLike, it->getMusicId());
         });
     }

     ui->pageMusicList->repaint();
}

void CommonPage::setMusicListType(PageType pageType)
{
     this->pageType = pageType;
}


void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicListOfPage.clear();
    for(auto& music : musicList)
    {
        switch(pageType)
        {
            case LOCAL_PAGE:
                musicListOfPage.push_back(music.getMusicId());
                break;
            case LIKE_PAGE:
            {
                if(music.getIsLike())
                {
                    musicListOfPage.push_back(music.getMusicId());
                }
                break;
            }
            case HISTORY_PAGE:
            {
                if(music.getIsHistory())
                {
                    musicListOfPage.push_back(music.getMusicId());
                    break;
                }
            }
            default:
                break;
        }
    }

}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto music : musicList)
        {
            switch(pageType)
            {
            case LOCAL_PAGE:
            {
                playList->addMedia(music.getMusicUrl());
                break;
            }
            case LIKE_PAGE:
            {
                if(music.getIsLike())
                {
                    playList->addMedia(music.getMusicUrl());
                }
                break;
            }
            case HISTORY_PAGE:
            {
                if(music.getIsHistory())
                {
                    playList->addMedia(music.getMusicUrl());
                }
                break;
            }
            default:
                break;
            }
    }
}
void CommonPage::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}
const QString &CommonPage::getMusicIdByIndex(int index) const
{
    if(index >= musicListOfPage.size())
     {
        qDebug()<<"no this song";
        return " ";
     }
    return musicListOfPage[index];
}



CommonPage::~CommonPage()
{
    delete ui;
}
